<template>
  <basic-container>
    <el-row :gutter="10">
      <el-col :span="8">
        <el-input
          v-model="content"
          size="small"
          placeholder="输入设备编号/房间名/所属客户/用户/联系方式"
        ></el-input>
      </el-col>
      <el-col :span="6">
        <el-button type="success" size="small" plain @click="searchChange"
          >搜 索</el-button
        >
        <el-button size="small" plain @click="searchReset">清 空</el-button>
      </el-col>
    </el-row>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page="page"
      :permission="permissionList"
      v-model="form"
      ref="crud"
      @row-save="rowSave"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
    >
      <template slot="menuLeft">
        <!-- <el-button
          type="success"
          size="small"
          icon="el-icon-add"
          plain
          v-if="permission.device_delete"
          @click="add"
        >新 增</el-button> -->
      </template>

      <template slot-scope="{ row }" slot="menu">
        <el-link @click="getDetail(row)" style="font-size: 13px" type="primary"
          >设备详情&nbsp;</el-link
        >
        <el-dropdown style="line-height: 32px">
          <el-link style="font-size: 12px; padding-top: 2px" type="primary"
            >| 更多</el-link
          >
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item>
              <span @click="drawerShow(row)">用电明细</span>
            </el-dropdown-item>
            <el-dropdown-item>
              <span @click="getelectricity(1)">通电</span>
            </el-dropdown-item>
            <el-dropdown-item>
              <span @click="getelectricity(0)">断电</span>
            </el-dropdown-item>
            <el-dropdown-item>
              <span @click="getelectricity(2)">设备日志</span>
            </el-dropdown-item>
            <el-dropdown-item>
              <span @click="rowUpdate(row)">修改</span>
            </el-dropdown-item>
            <el-dropdown-item>
              <span @click="rowDel(row)">删除</span>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </template>
    </avue-crud>
    <singlePhaseForm
      :singlePhaseData="singlePhaseData"
      :drawer.sync="drawer"
    ></singlePhaseForm>
    <addDevice
      :type="type"
      :deviceData="deviceData"
      :addDeviceShow.sync="addDeviceShow"
    ></addDevice>
    <!-- 设备详情 -->
    <el-dialog
      :append-to-body="true"
      title="设备详情"
      :visible.sync="dialogTableVisible"
    >
      <el-row class="conent">
        <!-- 设备类型 -->
        <!-- <div>
          <el-col :span="24">
            <div class="box_border">设备类型</div>
            <div class="box_border">{{deviceData.deviceType}}</div>
          </el-col>
        </div>-->
        <!-- 设备编号 -->
        <div>
          <el-col :span="24">
            <div class="box_border">设备编号</div>
            <div class="box_border">{{ deviceData.sn }}</div>
          </el-col>
        </div>
        <!-- 是否用户分摊 -->
        <div>
          <el-col :span="24">
            <div class="box_border">是否用户分摊</div>
            <div class="box_border">
              {{ deviceData.apportioned ? deviceData.apportionedName : "--" }}
            </div>
          </el-col>
        </div>
        <!-- 状态 -->
        <div>
          <el-col :span="24">
            <div class="box_border">安装状态</div>
            <div class="box_border">
              {{
                deviceData.installStatus ? deviceData.installStatusName : "--"
              }}
            </div>
          </el-col>
        </div>
        <!-- 规格 -->
        <div>
          <el-col :span="24">
            <div class="box_border">规格</div>
            <div class="box_border">
              {{ deviceData.spec ? deviceData.specName : "--" }}
            </div>
          </el-col>
        </div>
        <!-- 是否在线 -->
        <div>
          <el-col :span="24">
            <div class="box_border">是否在线</div>
            <div class="box_border">
              {{ deviceData.status ? deviceData.statusName : "--" }}
            </div>
          </el-col>
        </div>
      </el-row>
    </el-dialog>
  </basic-container>
</template>

<script>
import { getList, getDetail, add, update, remove } from "@/api/device/device";
import { mapGetters } from "vuex";
import singlePhaseForm from "./components/singlePhaseForm";
import addDevice from "./components/addDevice";

export default {
  components: {
    singlePhaseForm,
    addDevice,
  },
  data() {
    return {
      content: "",
      type: "add",
      id: "",
      singlePhaseData: {},
      deviceData: {},
      form: {},
      query: {},
      drawer: false,
      addDeviceShow: false,
      dialogTableVisible: false,
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      option: {
        height: "auto",
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        index: true,
        indexLabel: "序号",
        viewBtn: false,
        addBtn: false,
        editBtn: false,
        delBtn: false,
        selection: false,
        dialogClickModal: false,
        column: [
          // {
          //   label: "设备ID",
          //   prop: "id",
          //   rules: [
          //     {
          //       required: true,
          //       message: "请输入设备ID",
          //       trigger: "blur",
          //     },
          //   ],
          // },
          {
            label: "设备编码",
            prop: "sn",
            rules: [
              {
                required: true,
                message: "请输入设备编码",
                trigger: "blur",
              },
            ],
          },
          // {
          //   label: "设备规格",
          //   prop: "spec",
          //   type: "select",
          //   dicUrl: "/api/blade-system/dict/dictionary?code=equipmentspec",
          //   props: {
          //     label: "dictValue",
          //     value: "dictKey",
          //   },
          // },
          // {
          //   label: "设备类型",
          //   prop: "deviceType",
          //   type: "select",
          //   dicUrl: "/api/blade-system/dict/dictionary?code=equipmenttype",
          //   props: {
          //     label: "dictValue",
          //     value: "dictKey",
          //   },
          // },
          // {
          //   label: "设备名称",
          //   prop: "title",
          // },
          // {
          //   label: "规格",
          //   prop: "spec",
          // },
          // {
          //   label: "是否用户分摊",
          //   prop: "apportioned",
          //   rules: [
          //     {
          //       required: true,
          //       message: "请输入是否用户分摊",
          //       trigger: "blur",
          //     },
          //   ],
          // },
          {
            label: "房间名称",
            viewDisplay: false,
            prop: "roomname",
          },
          // {
          //   label: "房间号",
          //   viewDisplay: false,
          //   prop: "roomno",
          // },
          {
            label: "所属客户",
            prop: "customerName",
            viewDisplay: false,
            rules: [
              {
                required: true,
                message: "请输入租客名",
                trigger: "blur",
              },
            ],
          },
          {
            label: "用户",
            prop: "membername",
            viewDisplay: false,
          },
          {
            label: "联系方式",
            prop: "membermobile",
            rules: [
              {
                required: true,
                message: "请输入联系方式",
                trigger: "blur",
              },
            ],
          },
          {
            label: "账户余额",
            prop: "balance",
            rules: [
              {
                required: true,
                message: "请输入账户余额",
                trigger: "blur",
              },
            ],
          },
        ],
      },
      data: [],
    };
  },
  watch: {
    addDeviceShow() {
      if (!this.addDeviceShow) {
        console.log("watch");
        this.onLoad(this.page);
      }
    },
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.device_add, false),
        viewBtn: this.vaildData(this.permission.device_view, false),
        delBtn: this.vaildData(this.permission.device_delete, false),
        editBtn: this.vaildData(this.permission.device_edit, false),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  created() {
    if (this.$route.query.id) {
      this.id = this.$route.query.id;
      this.onLoad(this.page, { id: this.id });
    } else {
      this.onLoad(this.page);
    }
  },
  methods: {
    drawerShow(row) {
      this.singlePhaseData = row;
      this.drawer = true;
    },
    add() {
      console.log("addd");
      this.type = "add";
      this.addDeviceShow = true;
    },
    getelectricity(val) {
      if (val == 2) {
        this.$router.push({
          path: "/device/deviceLog",
        });
        return;
      }
      console.log("val", val);
      this.$confirm("是否进行此操作?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        return this.$notify({
          message: "操作成功",
          type: "success",
        });
      });
    },
    rowSave(row, done, loading) {
      console.log(row);
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row) {
      getDetail(row.sn).then((res) => {
        this.deviceData = res.data.data;
        this.type = "update";
        this.addDeviceShow = true;
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    getDetail(row) {
      getDetail(row.sn).then((res) => {
        const data = res.data.data;
        if (data.spec == 1) {
          data.specName = "单相电表";
        } else if (data.spec == 2) {
          data.specName = "三相电表";
        }
        if (data.apportioned == 1) {
          data.apportionedName = "未分摊";
        } else if (data.apportioned == 2) {
          data.apportionedName = "已分摊";
        }
        if (data.installStatus == 1) {
          data.installStatusName = "已安装";
        } else if (data.installStatus == 0) {
          data.installStatusName = "未安装";
        }
        if (data.status == 1) {
          data.statusName = "在线";
        } else if (data.status == 0) {
          data.statusName = "离线";
        }
        this.deviceData = data;
        this.dialogTableVisible = true;
      });
    },
    searchReset() {
      this.content = "";
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = { content: this.content };
      this.page.currentPage = 1;
      this.onLoad(this.page, this.query);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.onLoad(this.page, this.query);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.onLoad(this.page, this.query);
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      let obj = {
        deviceType: 1,
      };
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query, obj)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.box_border {
  border: 1px solid #e2e2e2;
  text-align: center;
  line-height: 35px;
  box-sizing: border-box;
  width: 50%;
  float: left;
}

.conent {
  .el-col {
    margin-bottom: 0;
  }
}
</style>
